Completed
Pull Request — develop (#75)
by
unknown
01:05
created

draw.js ➔ define   B

Complexity

Conditions 1
Paths 12

Size

Total Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 12
dl 0
loc 106
rs 8.2857
nop 1

8 Functions

Rating   Name   Duplication   Size   Complexity  
A draw.js ➔ ... ➔ setTransform 0 3 1
A draw.js ➔ ... ➔ setHighlight 0 3 1
A draw.js ➔ ... ➔ drawNode 0 19 4
A draw.js ➔ ... ➔ drawHighlightNode 0 8 4
A draw.js ➔ ... ➔ drawDetailNode 0 16 4
A draw.js ➔ ... ➔ setCTX 0 3 1
A draw.js ➔ ... ➔ drawHighlightLink 0 10 4
A draw.js ➔ ... ➔ drawLink 0 18 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict';
2
3
define(['helper'], function (helper) {
4
  var self = this;
5
6
  var ctx;
7
  var transform;
8
9
  var highlight;
10
11
  const nonUplinkColor = '#F2E3C6';
12
  const locationColor = '#00ff00';
13
  const noLocationColor = '#0000ff';
14
  const clientColor = 'rgba(230, 50, 75, 1.0)';
15
16
  const cableColor = '#50B0F0';
17
  const highlightColor = 'rgba(255, 255, 255, 0.2)';
18
19
  const NODE_RADIUS = 15;
20
  const LINE_RADIUS = 12;
21
22
  function drawDetailNode(d) {
23
    if (transform.k > 1) {
24
      ctx.beginPath();
25
      helper.positionClients(ctx, d, Math.PI, d.o.node.statistics.clients, 15);
26
      ctx.fillStyle = clientColor;
27
      ctx.fill();
28
      ctx.beginPath();
29
      var name = d.o.node_id;
30
      if (d.o.node && d.o.node.nodeinfo) {
31
        name = d.o.node.nodeinfo.hostname;
32
      }
33
      ctx.textAlign = 'center';
34
      ctx.fillStyle = '#fff';
35
      ctx.fillText(name, d.x, d.y + 20);
36
    }
37
  }
38
39
  function drawHighlightNode(d) {
40
    if (highlight && highlight.type === 'node' && d.o.node === highlight.o) {
41
      ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI);
42
      ctx.fillStyle = highlightColor;
43
      ctx.fill();
44
      ctx.beginPath();
45
    }
46
  }
47
48
  function drawHighlightLink(d) {
49
    if (highlight && highlight.type === 'link' && d.o === highlight.o) {
50
      ctx.lineTo(to[0], to[1]);
51
      ctx.strokeStyle = highlightColor;
52
      ctx.lineWidth = LINE_RADIUS * 2;
53
      ctx.lineCap = 'round';
54
      ctx.stroke();
55
      to = [d.source.x, d.source.y];
0 ignored issues
show
Bug introduced by
The variable to seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.to.
Loading history...
56
    }
57
  }
58
59
  self.drawNode = function drawNode(d) {
60
    ctx.beginPath();
61
62
    drawHighlightNode(d);
63
64
    ctx.moveTo(d.x + 3, d.y);
65
    ctx.arc(d.x, d.y, 6, 0, 2 * Math.PI);
66
    ctx.fillStyle = noLocationColor;
67
    if (d.o.node && d.o.node.nodeinfo && d.o.node.nodeinfo.location) {
68
      ctx.fillStyle = locationColor;
69
    }
70
    ctx.strokeStyle = nonUplinkColor;
71
    ctx.lineWidth = 5;
72
    ctx.globalAlpha = 1;
73
    ctx.fill();
74
    ctx.stroke();
75
76
    drawDetailNode(d);
77
  };
78
79
  self.drawLink = function drawLink(d) {
80
    ctx.beginPath();
81
    ctx.moveTo(d.source.x, d.source.y);
82
    var to = [d.target.x, d.target.y];
83
84
    drawHighlightLink(d);
85
86
    ctx.lineTo(to[0], to[1]);
87
    ctx.strokeStyle = d.o.type === 'Kabel' ? cableColor : d.color;
88
    if (d.o.type === 'fastd' || d.o.type === 'L2TP') {
89
      ctx.globalAlpha = 0.2;
90
      ctx.lineWidth = 1.5;
91
    } else {
92
      ctx.globalAlpha = 0.8;
93
      ctx.lineWidth = 2.5;
94
    }
95
    ctx.stroke();
96
  };
97
98
  self.setCTX = function setCTX(newValue) {
99
    ctx = newValue;
100
  };
101
  self.setHighlight = function setHighlight(newValue) {
102
    highlight = newValue;
103
  };
104
  self.setTransform = function setTransform(newValue) {
105
    transform = newValue;
106
  };
107
  return self;
108
});
109